package com.satan.upload.controller;

import com.satan.upload.service.FileService;
import com.satan.upload.vo.ResultVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName FileController
 * @Description TODO
 * @date 2023/9/13 14:35
 * @Version 1.0
 */
@RestController
@RequestMapping("file")
public class FileController {
    @Autowired
    private FileService fileService;
    //文件是否存在接口
    @GetMapping("check")
    public ResultVo checkFile(@RequestParam("md5") String md5) {
        return fileService.checkFile(md5);
    }
    //上传分片接口
    @PostMapping("upload/chunk")
    public ResultVo uploadChunk(@RequestParam("chunk") MultipartFile chunk,
                                @RequestParam("md5") String md5,
                                @RequestParam("index") Integer index,
                                @RequestParam("chunkTotal") Integer chunkTotal,
                                @RequestParam("fileSize") Long fileSize,
                                @RequestParam("fileName") String fileName,
                                @RequestParam("chunkSize") Long chunkSize
    ) {
        return fileService.uploadChunk(chunk, md5, index, chunkTotal, fileSize, fileName, chunkSize);
    }
    @GetMapping("list")
    public ResultVo getFileList(){

        return  fileService.getFileList();
    }

    @PostMapping("/download")
    public void download(@RequestParam("md5") String md5,
                         @RequestParam("fileName") String fileName,
                         @RequestParam("chunkSize") Integer chunkSize,
                         @RequestParam("chunkTotal") Integer chunkTotal,
                         @RequestParam("index")Integer index,
                         HttpServletResponse response) {
        fileService.download(md5,fileName,chunkSize,chunkTotal,index,response);

    }


}
